#include<iostream>
#include<string>
using namespace std;
bool isWin(string cur_s,int cur_i){
    if(cur_s[cur_i-2] == 'W' && cur_s[cur_i-1] == 'W'&& cur_s[cur_i] == 'W')
        return true;
    else return false;
}
int main(){
    int t;
    cin >> t;
    while(t--){
        string s;
        int n,k;
        long long sum = 0;
        cin >> n >> k;
        cin >> s;
        if(s[0] == 'W') sum++;
        else sum--;
        if(s[1] == 'W') sum++;
        else sum--;
        for(int i = 2;i < n;i++){
            if(isWin(s,i)){
                sum += k;
            }
            else {
                (s[i] == 'W')? sum++:sum--;
            }
        }
    cout << sum << endl;
    }
}